package com.suitsoft.ut.web.query;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;

import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.google.common.collect.Lists;
import com.suitsoft.ut.entity.thesis.Thesis;
import com.suitsoft.ut.entity.thesis.ThesisFile;
import com.suitsoft.ut.util.upload.UploaderDBUtil;
import com.suitsoft.ut.util.upload.XdbFiles;
import com.suitsoft.ut.web.InitListActionSupport;

/**
 * 查询
 *@author Suine
 */
public class QueryAction extends InitListActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = 514381027835076197L;
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	/** 上传文件列表 */
	private List<XdbFiles> xdbFiles = Lists.newArrayList();
	/** 查看 */
	private final String VIEW = "view";
	/** 文件ID */
	private Long fid;
	
	/**
	 * 查看
	 * @return
	 */
	public String view(){
		if(id == null || id == 0l){
			addActionMessage("非法操作");
		}
		UploaderDBUtil dbUtil = new UploaderDBUtil(ServletActionContext.getServletContext());
		xdbFiles = dbUtil.getFilesByUid(id.toString(),UploaderDBUtil.ALL);
		if(xdbFiles == null ||xdbFiles.isEmpty()){
			addActionMessage("无数据");
		}
		return SUCCESS;
	}
	
	public String download(){
		if(fid == null || fid == 0l){
			addActionMessage("非法操作");
			return SUCCESS;
		}
		UploaderDBUtil dbUtil = new UploaderDBUtil(ServletActionContext.getServletContext());
		XdbFiles files = dbUtil.getFileInfByFid(Integer.valueOf(fid.toString()));
		try {
			Struts2Utils.getRequest().setAttribute("exportFilePath", files.getFilePathLocal());
			Struts2Utils.getRequest().setAttribute("exportFileName", files.getFileNameRemote());
			Struts2Utils.getRequest().getRequestDispatcher("/download.jsp").forward(Struts2Utils.getRequest(), Struts2Utils.getResponse());
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Thesis getThesis() {
		return thesis;
	}
	public void setThesis(Thesis thesis) {
		this.thesis = thesis;
	}
	public ThesisFile getThesisFile() {
		return thesisFile;
	}
	public void setThesisFile(ThesisFile thesisFile) {
		this.thesisFile = thesisFile;
	}

	public List<XdbFiles> getXdbFiles() {
		return xdbFiles;
	}

	public void setXdbFiles(List<XdbFiles> xdbFiles) {
		this.xdbFiles = xdbFiles;
	}

	public Long getFid() {
		return fid;
	}

	public void setFid(Long fid) {
		this.fid = fid;
	}
}

